import { _decorator, Component, Animation, AnimationClip, SpriteFrame } from 'cc';
import { ENTITY_STATE_ENUM, FSM_PARAMS_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enum';
import State from '../../common/State';
import { getInitParamsNumber, getInitParamsTrigger, StateMachine } from '../../common/StateMachine';
import { TurnRightSubStateMachine } from './TurnRightSubStateMachine';
import { TurnLeftSubStateMachine } from './TurnLeftSubStateMachine';
import { IdleSubStateMachine } from './IdleSubStateMachine';
import { BlockTurnRightSubStateMachine } from './BlockTurnRightSubStateMachine';
import { BlockTurnLeftSubStateMachine } from './BlockTurnLeftSubStateMachine';
import { BlockRightStateMachine } from './BlockRightStateMachine';
import { BlockFrontStateMachine } from './BlockFrontStateMachine';
import { BlockBackStateMachine } from './BlockBackStateMachine';
import { BlockLeftStateMachine } from './BlockLeftStateMachine';
import { DeadStateMachine } from './DeadStateMachine';
import { AttackStateMachine } from './AttackStateMachine';
import { EntiyManager } from '../../common/EnityManager';
import { AirDeadStateMachine } from './AirDeadStateMachine';
const { ccclass, property } = _decorator;


@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends StateMachine {

  async init(){
    this.animationComponent = this.addComponent(Animation)
    this.initStateMachines()
    this.initParams()
    this.initAnimationEvent()
    await Promise.all(this.waitingList)
  }

  initStateMachines(){
    this.stateMachine[PARAMS_NAME_ENUM.IDLE] = new IdleSubStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.TURNRIGHT] = new TurnRightSubStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.TURNLEFT] = new TurnLeftSubStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.BLOCKTURNLEFT] = new BlockTurnLeftSubStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.BLOCKTURNRIGHT] = new BlockTurnRightSubStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.BLOCKFRONT] = new BlockFrontStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.BLOCKBACK] = new BlockBackStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.BLOCKRIGHT] = new BlockRightStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.BLOCKLEFT] = new BlockLeftStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.DEAD] = new DeadStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.AIRDEAD] = new AirDeadStateMachine(this)
    this.stateMachine[PARAMS_NAME_ENUM.ATTACK] = new AttackStateMachine(this)
  }

  initParams(){
    this.params[PARAMS_NAME_ENUM.DIRECTION] = getInitParamsNumber()

    for (const stateKey of Object.keys(this.stateMachine)) {
      this.params[stateKey] = getInitParamsTrigger()
    }
  }



  initAnimationEvent(){
    this.animationComponent.on(Animation.EventType.FINISHED, () => {

      const name = this.animationComponent.defaultClip.name
      const whiteList = ['turn', 'block', 'attack']
      if(whiteList.some(v=> name.includes(v))){
        this.node.getComponent(EntiyManager).state = ENTITY_STATE_ENUM.IDLE
        // this.setParam(PARAMS_NAME_ENUM.IDLE, true)
      }

    })
  }


  run(){
    let isTrigger = false
    for (const stateKey of Object.keys(this.stateMachine)) {
      if (this.params[stateKey].value) {
        this.currentState = this.stateMachine[stateKey];
        isTrigger = true
        break;
      }
    }

    if (!isTrigger) {
      this.currentState = this.currentState? this.currentState : this.stateMachine[ENTITY_STATE_ENUM.IDLE]; // 保持当前状态不变
    }

  }




}
